package com.rovinj_server.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;


import com.rovinj_server.view.CrashHandler;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class MyApplication extends Application {
    private static Context context;//上下文
    private List<Activity> activityList = new LinkedList<Activity>();
    private List<Activity> singlepointActivityList = new LinkedList<Activity>();
    private static MyApplication instance;
    private static Thread mainThread;//主线程
    private static int mainThreadId;//主线程的id
    public static Handler mainHandler;

    public Activity mActivity;


    public static MyApplication getInstance() {
        return instance;
    }



    public Activity getCurrentActivity() {
        return mActivity;
    }

    @Override
    public void onCreate() {

        super.onCreate();

        instance = this;
        context = this.getApplicationContext();
        mainThread = Thread.currentThread();//取得当前的线程
        mainThreadId = android.os.Process.myTid();//取得当前的线程的id

     //   initEmailReporter();//发邮箱
        //异常捕捉监控
     /*  CrashHandler instance = CrashHandler.getInstance();
       instance.init(getApplicationContext());*/
     /*   initCrashReport();*/




        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
                mActivity = activity;
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });

        mainHandler = new Handler();
    }
/*    private void initCrashReport() {
        LogReport.getInstance()
                .setCacheSize(30 * 1024 * 1024)//支持设置缓存大小，超出后清空
                .setLogDir(getApplicationContext(), "sdcard/" + this.getString(this.getApplicationInfo().labelRes) + "/")//定义路径为：sdcard/[app name]/
                .setWifiOnly(true)//设置只在Wifi状态下上传，设置为false为Wifi和移动网络都上传
                .setLogSaver(new CrashWriter(getApplicationContext()))//支持自定义保存崩溃信息的样式
                //.setEncryption(new AESEncode()) //支持日志到AES加密或者DES加密，默认不开启
                .init(getApplicationContext());
        initEmail();
    }

    private void initEmail() {
        EmailReporter email = new EmailReporter(this);
        email.setReceiver("121238475@qq.com");//收件人
        email.setSender("972790120@qq.com");//发送人邮箱
        email.setSendPassword("tnooszlepuxlbgac");//邮箱密码
        email.setSMTPHost("smtp.qq.com");//SMTP地址
        email.setPort("465");//SMTP 端口
        LogReport.getInstance().setUploadType(email);
        LogReport.getInstance().upload(this);
    }*/

    public static Handler getHandler() {
        return mainHandler;
    }

    public static Thread getMainThread() {
        return mainThread;
    }

    public static int getMainThreadId() {
        return mainThreadId;
    }

    public static Context getContext() {
        return context;
    }

    /*
     * 公共变量
     */
    public Token token;

    public class Token {
        public String token_type;
        public String expires_in;
        public String refresh_token;
        public String access_token;

        public Token(String token_type, String expires_in, String refresh_token, String access_token) {
            this.token_type = token_type;
            this.expires_in = expires_in;
            this.refresh_token = refresh_token;
            this.access_token = access_token;
        }
    }

    // 添加Activity 到容器中
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    public void addSinglepointActivity(Activity activity) {
        singlepointActivityList.add(activity);
    }

    // 遍历所有Activity 并finish
    public void finishSinglepointActivity() {
        for (Activity activity : singlepointActivityList) {
            activity.finish();
        }
    }

    // 遍历所有Activity 并finish
    public void exit() {
        for (Activity activity : activityList) {
            activity.finish();
        }
//		rfidUtil.stopInventory();
        //    rfidUtil.onDestroy();
        System.exit(0);
    }

    //用户模块权限
    public Map userAuthor;
}
